package com.zhida.smartcontrol.pagectrl.editproject;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.jay.tools.utils.StringUtils;
import com.yanzhenjie.kalle.Params;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

import java.util.HashMap;
import java.util.Map;

/**
 * author: tanjianpei
 * date: 2019/10/9 009 16:27
 * description:
 */

public class EditProjectPresenter extends BasePresenterImpl<EditProjectContract.View> implements EditProjectContract.Presenter {

    @Override
    public void requestInfo(String id) {
        Params params = Params.newBuilder()
                .add("id", id).build();
        mKalleCenter.get(Api.PROJECT_ID, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                EditProjectBean bean = JSON.parseObject(result, EditProjectBean.class);
                mView.showInfo(bean);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void submit(int type, String id, String projectName, String manager, String address, String phone, String lat, String lng) {
        if (TextUtils.isEmpty(projectName)) {
            mView.showMsg("请输入项目名称");
            return;
        }
        if (TextUtils.isEmpty(address)) {
            mView.showMsg("请输入项目地址");
            return;
        }
        if (TextUtils.isEmpty(manager)) {
            mView.showMsg("请输入单位负责人姓名");
            return;
        }
        if (!StringUtils.checkMobile(phone)) {
            mView.showMsg("请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(lat) || TextUtils.isEmpty(lng)) {
            mView.showMsg("请选择经纬度");
            return;
        }
        String url;
        Map<String, Object> params = new HashMap<>();
        if (type == Const.TYPE_EDIT) {
            url = Api.PROJECT_EDIT;
            params.put("id", id);
        } else {
            url = Api.PROJECT_ADD;
        }
        params.put("projectName", projectName);
        params.put("address", address);
        params.put("managerName", manager);
        params.put("managerMobile", phone);
        params.put("lat", lat);
        params.put("lng", lng);
        mKalleCenter.putJson(url, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.onSuccess(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });

    }
}
